<template>
    <!-- 引入container布局 -->
    <el-container class="HomeContainer">
        <!-- 头部 -->
        <el-header>
            <div>
                <div class="divLeft">
                    <img src="../assets/logo.jpg" alt />
                </div>
                <div class="divRight" >
                    个人运动平台
                </div>
            </div>
            <el-button type="info" @click="logout">安全退出</el-button>
        </el-header>
        <el-container>
            <!-- 侧边栏 -->
            <el-aside width="200px">

                <el-menu background-color="#545c64" text-color="#fff" active-text-color="#409eff" unique-opened :router ="true" :default-active="activePath">
                    <!--一级菜单-->
                    <el-submenu :index="item.id+''" v-for="item in menulist" :key="item.id">
                        <template slot="title">
                            <i :class="iconsObject[item.id]"></i>
                            <span>{{item.title}}</span>
                        </template>
                        <!--二级菜单-->
                        <el-menu-item :index="it.path" v-for="it in item.sList" :key="it.id" @click="saveNavState(it.path)">
                            <template slot="title">
                                <i :class="iconsObject[it.id]"></i>
                                <span>{{it.title}}</span>
                            </template>
                        </el-menu-item>
                    </el-submenu>
                </el-menu>
            </el-aside>
            <!-- 主体内容 -->
            <el-main>
                <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>

</template>
<script>
export default {
    data() {
        return {
            //菜单列表
            menulist: [],
            //图标属性
            iconsObject:{
                '100':'iconfont icon-guanliyuan',
                '200':'iconfont icon-yundong-',
                '101':'iconfont icon-denglu',
                '102':'iconfont icon-mima',
                '103':'iconfont icon-yundong-',
                '104':'iconfont icon-shangpin',
                '201':'iconfont icon-shu',
                '202':'iconfont icon-kaluli',
                '203':'iconfont icon-shiwu',
                '204':'iconfont icon-denglu',
            },
            activePath:'/welcome',//默认路径
        }
    },
    //onload 事件
    created() {
        //查询menulist列表
        this.getMenulist();
        this.activePath=window.sessionStorage.getItem('activePath');//取出session里的path
    },
    methods: {
        logout() {
            window.sessionStorage.clear();//清除session
            this.$router.push("/login")//回到首页
        },
        //获取导航菜单方法
        async getMenulist() {
            const { data: res } = await this.$http.get("menus");
            console.log(res);
            if (res.flag != 200) return this.$message.error("获取列表失败!!!");//访问失败的错误信息
            this.menulist = res.menus;//数据回填
        },
        //保存路径
        saveNavState(activePath){
            window.sessionStorage.setItem('activePath',activePath);
            this.activePath=activePath;
        }
    }
}
</script>
<style scoped>
/* 布局样式 */
.HomeContainer {
    height: 100%;
}

/* 头部样式 */
.el-header {
    background-color: #2d3a42;
    display: flex;
    justify-content: space-between;
    padding-left: 0%;
    align-items: center;
    color: #fff;
    font-size: 20px;

}
.divLeft{
   float: left; 
   height: 60.45px;
}
.divRight{
    float: right;
    height: 60.45px;
    padding-top: 15px;
    padding-left: 10px;
}

/* 侧边栏样式 */
.el-aside {
    background-color: #2d3a42;
    .el-menu{
        border-right: none;
    }

}

/* 主体样式 */
.el-main {
    background-color: #E9EEF3;

}

/*|||按钮样式 */
.toggle-Button{
    background-color: #4a5064;
    font-size: 10px;
    line-height: 24px;
    color: #fff;
    text-align: center;/*水平居中*/
    letter-spacing: 0.2em;
    cursor: pointer;/*显示小手 */
}
</style>